package com.bnlr.lrparking.appui.sharevalue;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bnlr.lrparking.R;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.appui.approve.ApproveActivity;
import com.bnlr.lrparking.appui.sharevalue.deposit.DepositActivity;
import com.bnlr.lrparking.appui.sharevalue.deposit.DepositRecordActivity;
import com.bnlr.lrparking.appui.sharevalue.recharge.RechargeRecordActivity;
import com.bnlr.lrparking.appui.sharevalue.recharge.TopUpActivity;
import com.bnlr.lrparking.appui.mine.record.InvestRecordActivity;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.view.CarCommon;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/6.
 * 共享值 收益值
 *
 * @author hasee
 */
public class ShareActivity extends BaseActivity {
    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.tv_nav_right)
    TextView tvNavRight;
    @BindView(R.id.iv_nav_right)
    ImageView ivNavRight;
    @BindView(R.id.iv_nav_back)
    ImageView ivNavBack;
    @BindView(R.id.share_num)
    TextView shareNum;
    @BindView(R.id.earnings_num)
    TextView earningsNum;
    @BindView(R.id.tv_sharevalue_u)
    TextView tvSharevalueU;
    @BindView(R.id.tv_parktime)
    TextView tvParktime;
    @BindView(R.id.tv_exchange_rate)
    TextView tvExchangeRate;
    @BindView(R.id.tv_earnings_explain)
    TextView tvEarnExplain;
    private RelativeLayout include_share, include_earnings;
    private RelativeLayout rely_share, rely_earnings;
    private TextView tv_share, tv_earnings;
    private View line_share, line_earnings;
    private TextView tv_share_btn, tv_deposit_btn;//充值
    private RelativeLayout share_recharge, deposit_record, invest_record;//充值记录   提现，投资记录
    private String strshareNum, strSharevalueU, strParktime, strearningsNum;//剩余贡献值，已用贡献值，已停时间，剩余收益值
    private String isRealNameAuth = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
        getPersonInfo();
    }

    private void initView() {
        ButterKnife.bind(this);
        tvNavTitle.setText("共享值");
        tvExchangeRate.setText("1、" + LrConstant.RATE_VALUE + "贡献值=1元；");
        tvEarnExplain.setText("1、收益值为用户投资虚拟车位所得收益；\n2、" + LrConstant.RATE_VALUE + "收益值=1元；\n3、收益值可提现至微信或已绑定银行卡。");
        // getPersonInfo();
        include_share = (RelativeLayout) findViewById(R.id.include_share);
        include_earnings = (RelativeLayout) findViewById(R.id.include_earnings);
        rely_share = (RelativeLayout) findViewById(R.id.rely_share);
        rely_earnings = (RelativeLayout) findViewById(R.id.rely_earnings);
        tv_share = (TextView) findViewById(R.id.tv_share);
        tv_earnings = (TextView) findViewById(R.id.tv_earnings);
        line_share = (View) findViewById(R.id.line_share);
        line_earnings = (View) findViewById(R.id.line_earnings);
        tv_share_btn = (TextView) findViewById(R.id.tv_share_btn);
        tv_deposit_btn = (TextView) findViewById(R.id.tv_deposit_btn);
        share_recharge = (RelativeLayout) findViewById(R.id.share_recharge);
        deposit_record = (RelativeLayout) findViewById(R.id.deposit_record);
        invest_record = (RelativeLayout) findViewById(R.id.invest_record);

        rely_share.setOnClickListener(this);
        rely_earnings.setOnClickListener(this);
        tv_share_btn.setOnClickListener(this);
        tv_deposit_btn.setOnClickListener(this);
        share_recharge.setOnClickListener(this);
        deposit_record.setOnClickListener(this);
        invest_record.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        isRealNameAuth = AppConfig.getInstance().getString("isRealNameAuth", "");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.rely_share:
                include_share.setVisibility(View.VISIBLE);
                include_earnings.setVisibility(View.GONE);
                tv_share.setTextColor(getResources().getColor(R.color.main_color));
                tv_earnings.setTextColor(getResources().getColor(R.color.gray03_color));
                line_share.setVisibility(View.VISIBLE);
                line_earnings.setVisibility(View.GONE);
                break;
            case R.id.rely_earnings:
                include_share.setVisibility(View.GONE);
                include_earnings.setVisibility(View.VISIBLE);
                tv_share.setTextColor(getResources().getColor(R.color.gray03_color));
                tv_earnings.setTextColor(getResources().getColor(R.color.main_color));
                line_share.setVisibility(View.GONE);
                line_earnings.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_share_btn:
                startActivity(new Intent(this, TopUpActivity.class).putExtra("shouyi", earningsNum.getText().toString()));
                break;
            case R.id.tv_deposit_btn:
                CarCommon carCommon = new CarCommon();
                if (isRealNameAuth.equals("0")) {
                    carCommon.setClickSureListener(new CarCommon.ClickSureListener() {
                        @Override
                        public void setSure() {
                            startActivity(ApproveActivity.class);
                        }
                    });
                    carCommon.CommonDialog(ShareActivity.this, "请您先去进行实名认证", null);
                } else if (isRealNameAuth.equals("2")) {
                    carCommon.ReminderDialog(ShareActivity.this, "身份信息正在审核中，请稍候再试", null);
                } else if (isRealNameAuth.equals("3")) {
                    carCommon.ReminderDialog(ShareActivity.this, "您的实名认证未通过，请重新在个人中心认证", null);
                } else {
                    Intent intent = new Intent(ShareActivity.this, DepositActivity.class);
                    intent.putExtra("strearningsNum", strearningsNum);
                    startActivity(intent);
                }
                break;
            case R.id.share_recharge://充值记录
                startActivity(RechargeRecordActivity.class);
                break;
            case R.id.deposit_record://提现记录
                startActivity(DepositRecordActivity.class);
                break;
            case R.id.invest_record://投资记录
                startActivity(InvestRecordActivity.class);
                break;
            default:
                break;
        }
    }


    /**
     * 获取贡献值  收益值
     */
    private void getPersonInfo() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetUserInfoById");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            // 添加到请求队列
            CallServer.getRequestInstance().add(ShareActivity.this, mRequest,
                    new CustomHttpListener<JSONObject>(ShareActivity.this, false, null) {
                        @Override
                        public void doWork(JSONObject data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    JSONObject object = obj.getJSONObject("data");
                                    //使用的贡献值
                                    tvSharevalueU.setText(object.getString("sharevalue_u"));
                                    //停车时间
                                    tvParktime.setText(object.getString("parktime"));
                                    //贡献值
                                    shareNum.setText(object.getString("sharevalue") + "");
                                    //收益值
                                    earningsNum.setText(object.getString("incomevalue") + "");
                                    strearningsNum = object.getString("incomevalue");
                                } else {

                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {

        }
    }
}
